using System;
using System.Collections.Generic;
using System.Text;

namespace Eve.Data
{
    public class Agent : EveObject
    {
        private int divisionId;
        private int corporationId;
        private int stationId;
        private int level;
        private int quality;
        private int agentTypeId;

        private Division division;
        private Corporation corporation;
        private Station station;
        private AgentType agentType;

        private bool retrieveAllConfig = false;
        private Dictionary<string, AgentConfig> configuration = new Dictionary<string,AgentConfig>();

        public Division Division
        {
            get
            {
                if (division == null)
                    division = new Division(db, divisionId);
                return division; 
            }
        }
        public Corporation Corporation
        {
            get
            {
                if (corporation == null)
                    corporation = new Corporation(db, corporationId);
                return corporation;
            }
        }
        public Station Station
        {
            get
            {
                if (station == null)
                    station = new Station(db, stationId);
                return station;
            }
        }
        public int Level
        {
            get { return level; }
        }
        public int Quality
        {
            get { return quality; }
        }
        public AgentType AgentType
        {
            get
            {
                if (agentType == null)
                    agentType = new AgentType(db, agentTypeId);
                return agentType;
            }
        }
        public IEnumerable<AgentConfig> Configuration
        {
            get
            {
                if (!retrieveAllConfig) FillConfiguration();
                return configuration.Values;
            }
        }
        public IEnumerable<string> ConfigurationKeys
        {
            get
            {
                if (!retrieveAllConfig) FillConfiguration();
                return configuration.Keys;
            }
        }
        public AgentConfig this[string key]
        {
            get
            {
                if (!configuration.ContainsKey(key))
                {
                    string query = "SELECT * FROM agtConfig WHERE agentID=" + Id + " AND k='" + key + "'";
                    DbDataRow row = db.ExecuteSingle(query);
                    if (row != null) configuration[key] = new AgentConfig(row);
                    else return null;
                }
                return configuration[key];
            }
        }

        internal Agent(DbDataRow dr)
            : base(dr.Database, EveObjectType.Agent, dr["agentID"])
        {
            Fill(dr);
        }
        internal Agent(Database db, int id)
            : base(db, EveObjectType.Agent, id)
        {
            Fill(db.ExecuteSingle("SELECT * FROM agtAgents WHERE agentID=" + id));
        }

        private void FillConfiguration()
        {
            string query = "SELECT * FROM agtConfig WHERE agentID=" + Id;
            foreach (DbDataRow row in db.ExecuteQuery(query))
            {
                AgentConfig cfg = new AgentConfig(row, this);
                configuration[cfg.Key] = cfg;
            }
            retrieveAllConfig = true;
        }
        private void Fill(DbDataRow dr)
        {
            divisionId = dr["divisionID"];
            corporationId = dr["corporationID"];
            stationId = dr["stationID"];
            level = dr["level"];
            quality = dr["quality"];
            agentTypeId = dr["agentTypeID"];
        }
    }
}
